window.onload = () => {
    const electron = require('electron')
    const ipcRenderer = electron.ipcRenderer
    let stamp = document.querySelector('.stamp')
    let doctor = document.querySelector('.doctor')
    let medical = document.querySelector('.medical')
    let iccid = document.querySelector('.iccid')
    let input_date = document.querySelector('.input_date')
    let print_border = document.querySelector('.print_border')
    let reload = document.querySelector('.reload')
    let opationArr = []
    let fileTexts = []
    let unitprice_site = ''
    medical.onselect = function (e) {
        let tagClass = e.target.className
        let tagName = e.target.nodeName
        if (tagName == 'INPUT' && tagClass.indexOf('update_price') != -1) {
            let tagIndex = e.target.getAttribute('index')
            unitprice_site = tagIndex
        }
    }
    medical.oninput = function(e) {
        let val = e.target.value
        let end = val.substr(val.length-1, val.length)
        let sub_val = 0
        if (isNaN(end)) {
            sub_val = val.substr(0, val.length-1)
        } else {
            sub_val = val
        }
        let unitprice = fileTexts.text[unitprice_site][6]
        medical.querySelectorAll('.unitprint')[unitprice_site].value = sub_val * unitprice
    }
    input_date.value = CurentTime()
    iccid.innerHTML = getIccid()
    ipcRenderer.send('request_fileText', '')
    ipcRenderer.on('reply_fileText', (event, arg) => {
        if (arg.doctor) {
            doctor.value = arg.doctor
        }
        setBox(arg.text)
        fileTexts = arg
    })
    medical.addEventListener('click', function(e) {
        let tagClass = e.target.className
        let tagName = e.target.nodeName
        if (tagName == 'IMG') {
            let tagClass = e.target.parentNode.className
            let tagIndex = e.target.parentNode.getAttribute('index')
            if(tagClass.indexOf('selectOption') != -1) {
                if (opationArr.indexOf(tagIndex) != -1) {
                    opationArr.splice(opationArr.indexOf(tagIndex), 1)
                    e.target.parentNode.className = 'selectOption'
                } else {
                    opationArr.push(tagIndex)
                    e.target.parentNode.className += ' selectActive'
                }
            }
        }
        if (tagName == 'DIV' && tagClass.indexOf('selectOption') != -1) {
            let tagIndex = e.target.getAttribute('index')
            if (opationArr.indexOf(tagIndex) != -1) {
                opationArr.splice(opationArr.indexOf(tagIndex), 1)
                e.target.className = 'selectOption'
            } else {
                opationArr.push(tagIndex)
                e.target.className += ' selectActive'
            }
        }
        if (tagName == 'INPUT' && tagClass.indexOf('update_price') != -1) {
            let tagIndex = e.target.getAttribute('index')
            unitprice_site = tagIndex
        }
    }, false)
    stamp.addEventListener('click', (e) => {
        updateBoxPrint()
        ipcRenderer.send('click_print', doctor.value)
    }, false)
    reload.addEventListener('click', (e) => {
        ipcRenderer.send('click_reload', '')
    }, false)
    function CurentTime()
    {
        var now = new Date()
        var year = now.getFullYear()
        var month = now.getMonth() + 1
        var day = now.getDate()
        var hh = now.getHours()
        var mm = now.getMinutes()
        var clock = year + "-"
        if(month < 10)
            clock += "0"
        clock += month + "-"
        if(day < 10)
            clock += "0"
        clock += day + " "
        if(hh < 10)
            clock += "0"
        clock += hh + ":"
        if (mm < 10) clock += '0'
        clock += mm
        return(clock)
    }
    function getIccid() {
        let now = new Date()
        let year = now.getFullYear().toString().substr(2, 2)
        let month = now.getMonth() + 1
        month = (month < 10 ? '0' + month : month).toString()
        let iccid = 'HH' + year + month + getRandom()
        return iccid
    }
    function getRandom() {
        let iccid = Math.random()
        iccid = iccid.toString().substr(2, 4)
        return iccid
    }
    function setBox(data) {
        let medical = document.querySelector('.medical')
        for (let [index, val] of data.entries()) {
            let box_template = `<div class="box">
                <div class="section1 ml25">
                  <div style="width:100%;"><input type="text" value="${val[0]}" placeholder="健康查体" class="more super"></div>
                </div>
                <div class="section3 mlbig mt5" style="width: 75%;">
                  <div style="display:inline-block"><input type="text" value="${val[1]}" placeholder="50mg*6袋/盒" class="long border_none" readonly></div>
                  <div style="display:inline-block;margin-left:27px;"><input type="text" value="${val[2]}" index=${index} placeholder="2盒" class="update_price"></div>
                </div>
                <div class="section1 mlbig mt3">
                  <div><input type="text" value="${val[3]}" placeholder="bid" readonly class="border_none"></div>
                  <div><input type="text" value="${val[4]}" placeholder="100mg" readonly class="border_none"></div>
                  <div><input type="text" value="${val[5]}" placeholder="口服" readonly class="border_none"></div>
                  <div>
                    <label for="" style="font-size: 12px;">单价￥</label>
                    <input type="text" value="${val[6]}" index=${index} placeholder="26" class="unitprint">
                  </div>
                </div>
                <div class="selectOption" index=${index}><img src="selected.png" alt="" readonly></div>
              </div>`;
            medical.insertAdjacentHTML('beforeend', box_template)
        }
    }
    function updateBoxPrint() {
        let appStyle = document.createElement('style')
        let addPrint = ''
        for (let val of opationArr) {
            addPrint += `.medical div.box:nth-child(${++val}) {display:block;}`
        }
        appStyle.innerHTML = `@media print {${addPrint}}`
        window.document.head.appendChild(appStyle)
    }
}